package com.atguigu.util;

import org.apache.hadoop.hbase.client.Connection;

import java.io.IOException;
import java.util.LinkedList;

public class HBaseDataSourceUtil {

    private LinkedList<Connection> connectionList;

    public HBaseDataSourceUtil() throws IOException {
        connectionList = new LinkedList<>();
        for (int i = 0; i < 5; i++) {
            connectionList.add(HBaseUtil.getConnection());
        }
    }

    public Connection getConnection() throws IOException {
        synchronized (HBaseDataSourceUtil.class) {
            if (connectionList.size() > 0) {
                return connectionList.removeFirst();
            } else {
                return HBaseUtil.getConnection();
            }
        }
    }

    public void addBack(Connection connection) {
        synchronized (HBaseDataSourceUtil.class) {
            connectionList.add(connection);
        }
    }

    public static void main(String[] args) throws IOException {
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("a");
        linkedList.add("a");

        linkedList.removeFirst();
        linkedList.removeFirst();
        linkedList.removeFirst();
    }
}
